<?php
$url = '';
$dir = __DIR__;
require_once './AbInput.php';
$input = new AbInput();
$input->setUrl(end($argv));
if ($url = parse_url($input->url())) {
    $scheme = $url['scheme'] ?? '';
    if (!$scheme || !in_array($scheme, ['http', 'https'])) {
        echo $input->url() . ' invalid URL!';
        return;
    }
}
$input->setRequests(10);
foreach ($argv as $key => $item) {
    if ($item === '-d' && $postDataArgs = $argv[$key + 1]) {
        if (stripos($postDataArgs, '=') !== false) {
            parse_str($argv[$key + 1], $postData);
            $input->postData = $postData;
        }
    }
    if ($item === '-X') {
        $input->method = strtoupper($argv[$key + 1]) === 'POST' ? "POST" : "GET";
    }

    if ($item === '-n') {
        $input->setRequests(1 * $argv[$key + 1]);
    }

    if ($item === '-c') {
        $input->setConcurrency(1 * $argv[$key + 1]);
    }
}
$command = $dir . DIRECTORY_SEPARATOR . "{$input->abExe()} {$input->buildVal()}  {$input->url()}";
echo "=============The command line==============\r\n";
echo "{$command}\r\n";
echo "=============================\r\n";
try {
    exec($command, $res);
    if (!$res) {
        echo '测试异常!';
    }
    foreach ($res as $item) {
        echo $item . "\r\n";
    }
} catch (Exception $exception) {
    echo $exception->getMessage();
} finally {
    $input->finish();
}
